/**
 * 列表控件 依赖文件： jquery/jquery-1.11.2.min.js meeting/meeting.core.js
 * bootstrap-table/1.11.0/bootstrap-table.js
 * bootstrap-table/1.11.0/bootstrap-table-zh-CN.js
 */
(function (mt) {
    mt.grid = function (el, cols, data, option) {
        var defaultOption = {
            striped: false, // 是否显示行间隔色
            pagination: true, // 是否显示分页（*）
            sortable: true, // 是否启用排序
            clickToSelect: true, // 是否启用点击选中行
            height: $(window).height() - 200, // 固定高度
            smartDisplay: false,
            columns: cols,
            data: data,
            pageSize: 20,
            pageList: [10, 20, 50, 100],
            toolbar: "#toolbar",
            toolbarAlign: "right",
            toolbarVisible: true
            /*            showExport: true,
                        exportDataType: "all",
                        exportTypes: ["excel"]*/
        }

        $.extend(defaultOption, option);

        //添加工具栏
        if (defaultOption.toolbarVisible) {
            var toolbarhtml = '<div id="toolbar" class="btn-group">' +
                '                  <button id="btnPersonCol" type="button" class="btn btn-default" onclick="meeting.grid.settingPersonGridCol(\'' + el + '\')">' +
                '                      <i class="glyphicon glyphicon-list"></i>' +
                '                  </button>' +
                '              </div>';

            $(el).before(toolbarhtml);
        }

        //加载列排序设置
     //   defaultOption.columns = loadPersonColSetting(defaultOption.columns);

        var grid = $(el).bootstrapTable(defaultOption);
        return grid;
    }

    //设置个人grid排序
    mt.grid.settingPersonGridCol = function (el) {

        //获取弹窗内容
        function getcontent() {
            var self = this;
            return $.ajax({
                type: "POST",
                url: proName + "/grid/gridtool/personcolsetting",
                data: {
                    visibleCol: JSON.stringify($(el).bootstrapTable("getVisibleColumns")),
                    hiddenCol: JSON.stringify($(el).bootstrapTable("getHiddenColumns"))
                }
            }).done(function (response) {
                self.setContent(response);
            })
        }

        var confirmOption = {
            title: "",
            boxWidth: 200,
            useBootstrap: false,
            content: getcontent,
            onOpenBefore: function () {

            },
            contentLoaded: function (data, status, xhr) {

            },
            onContentReady: function () {
                //this.$content.find(".contentidHid" + defaultOption.index).val(this.$content.attr("id"));
                //defaultOption.callback.loaded();
            },
            onClose: function () {
                //defaultOption.callback.close();
            },
            buttons: {
                save: {
                    text: "确认",
                    btnClass: 'btn-blue',
                    action: function () {
                        //保存列设置
                        var cols = [];
                        var curpage = window.location.href.replace(window.location.origin, "");
                        this.$content.find(".collist .colitem").each(function (i) {
                            var field = $(this).attr("field");
                            var isvisible = $(this).find(".isshow input").is(":checked") ? 1 : 0;
                            cols.push({page: curpage, field: field, visible: isvisible, sn: i + 1});
                        })

                        var result = false;
                        $.ajax({
                            async: false,
                            type: "POST",
                            url: proName + "/grid/gridtool/personcolsettingsave",
                            data: {
                                cols: JSON.stringify(cols),
                                page: curpage
                            },
                            success: function (data) {
                                var resultContent = JSON.parse(data);
                                if (Number(resultContent.state) > 0) {
                                    result = true;
                                    //提示刷新
                                    layer.msg("保存成功，请刷新页面加载最新设置", {
                                        icon: 1
                                    });
                                    //meeting.grid.colsn(el, cols);
                                }
                            }
                        })

                        return result;
                    }
                },
                cancel: {
                    text: "取消",
                    action: function () {
                        //defaultOption.callback.cancel();
                    }
                }
            }
        }

        //打开弹窗
        $.confirm(confirmOption)
    }

    //加载个人列配置
    function loadPersonColSetting(cols) {
        var curpage = window.location.href.replace(window.location.origin, "");
        $.ajax({
            async: false,
            type: "POST",
            url: proName + "/grid/gridtool/loadpersoncolsetting",
            data: {
                page: curpage
            },
            success: function (data) {
                var data = JSON.parse(data).data;

                for (var i = 0; i < data.length; i++) {
                    for (var j = 0; j < cols.length; j++) {
                        if (data[i].field == cols[j].field) {
                            cols[j].visible = data[i].visible == 1 ? true : false;
                            cols[j].sn = data[i].sn;
                        }
                    }
                }

                cols.sort(function (a, b) {
                    var fixTitle = ["选择", "操作"];
                    if ($.inArray(a.title, fixTitle) > -1 || $.inArray(b.title, fixTitle) > -1) {
                        return -1;
                    }
                    if (!a.sn) {
                        return 100;
                    }
                    if (!b.sn) {
                        return -1;
                    }

                    return a.sn - b.sn;
                })
            }
        })

        return cols;
    }
}(meeting));